package com.jeek.jdbc.myCode.homeWork2;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

public class CustomerMain {
    private static final Scanner scanner = new Scanner(System.in);
    private static final CustomerService customerService = new CustomerService();
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n===== 客户管理系统 =====");
            System.out.println("1. 插入客户数据");
            System.out.println("2. 修改指定客户");
            System.out.println("3. 查询所有客户信息");
            System.out.println("4. 根据编号查询客户详细信息");
            System.out.println("5. 根据姓名查询客户详细信息");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作：");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 吸收换行符

            try {
                switch (choice) {
                    case 1:
                        insertCustomer();
                        break;
                    case 2:
                        updateCustomer();
                        break;
                    case 3:
                        queryAllCustomers();
                        break;
                    case 4:
                        queryCustomerById();
                        break;
                    case 5:
                        queryCustomerByName();
                        break;
                    case 0:
                        System.out.println("系统已退出");
                        return;
                    default:
                        System.out.println("无效操作，请重新选择！");
                }
            } catch (Exception e) {
                System.out.println("操作失败：" + e.getMessage());
            }
        }
    }

    // 插入客户数据
    private static void insertCustomer() throws ParseException {
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入邮箱：");
        String email = scanner.nextLine();
        System.out.print("请输入生日（格式：yyyy-MM-dd）：");
        String birthday = scanner.nextLine();
        sdf.parse(birthday); // 验证日期格式

        Customer customer = new Customer(name, email, birthday);
        boolean result = customerService.insert(customer);
        if (result) {
            System.out.println("插入成功！系统已自动生成客户编号");
        } else {
            System.out.println("插入失败");
        }
    }

    // 修改指定客户
    private static void updateCustomer() {
        System.out.print("请输入待修改的客户编号：");
        int id = scanner.nextInt();
        scanner.nextLine();
        System.out.print("请输入新的客户姓名：");
        String newName = scanner.nextLine();

        Customer customer = new Customer();
        customer.setId(id);
        customer.setName(newName);
        boolean result = customerService.update(customer);
        if (result) {
            System.out.println("修改成功！");
        } else {
            System.out.println("未找到该编号的客户或修改失败");
        }
    }

    // 查询所有客户信息
    private static void queryAllCustomers() {
        List<Customer> customers = customerService.findAll();
        if (customers.isEmpty()) {
            System.out.println("暂无客户信息");
        } else {
            System.out.println("编号\t姓名\t邮箱\t\t生日");
            for (Customer c : customers) {
                System.out.printf("%d\t%s\t%s\t%s\n",
                        c.getId(), c.getName(), c.getEmail(), c.getBirthday());
            }
        }
    }

    // 根据编号查询客户详细信息
    private static void queryCustomerById() {
        System.out.print("请输入编号：");
        int id = scanner.nextInt();
        scanner.nextLine();

        Customer customer = customerService.findById(id);
        if (customer != null) {
            System.out.println("-----------------------------------------");
            System.out.printf("%-5s %-5s %-10s %-8s\n", "编号", "姓名", "邮箱", "生日");
            System.out.printf("%-5d %-5s %-10s %-8s\n",
                    customer.getId(), customer.getName(), customer.getEmail(), customer.getBirthday());
        } else {
            System.out.println("查无此人");
        }
    }

    // 根据姓名查询客户详细信息
    private static void queryCustomerByName() {
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();

        List<Customer> customers = customerService.findByNameLike(name);
        if (customers.isEmpty()) {
            System.out.println("查无此人");
        } else {
            System.out.println("-----------------------------------------");
            System.out.printf("%-5s %-5s %-10s %-8s\n", "编号", "姓名", "邮箱", "生日");
            for (Customer c : customers) {
                System.out.printf("%-5d %-5s %-10s %-8s\n",
                        c.getId(), c.getName(), c.getEmail(), c.getBirthday());
            }
        }
    }
}